
<?php
//header("Content-Type:text/html");
//$-POST $_GET: 在后端获取前端页面提交数据的全局数组
$userName = trim($_POST['username']);
$pw = trim($_POST['pw']);
$cpm = trim($_POST['cpm']);
$sex = $_POST['sex'];
$fav = implode(",", $_POST['fav']);
$email = $_POST['email'];
$source = $_POST['source'];
$page = $_POST['page'];
//echo "你输入的用户名：".$userName."<br>";
//echo  "你输入的密码:$pw <br>";
//echo  "你确认的密码:$cpm <br>";
//echo "输入的性别是：" ;
////echo "<br>";
//echo  $sex ==1 ? '男': '女';
//
//if($fav == 0){
//    echo "<br>你没有选择爱好";
//}else{
//    echo "<br>选择的爱好: ";
//    $fav1 = implode( ',',$fav);
//    echo $fav1;
//}

//第一步链接数据库

//$conn = mysqli_connect("member", "root", "root", "member");
//
//if(!$conn){
//    die("数据库连接失败");
//}
////第二步： 设置字符集
//mysqli_query($conn,"set names utf8");
include_once "conn.php";
//进行数据的验证
if(!strlen($userName)){
    echo "<script>alert('用户名和密码必须填写');
     history.back()
</script>";
    exit;

}else{
    if(!preg_match('/^[a-zA-Z0-9_]{3,10}$/', $userName)){
        echo "<script>alert('用户名必须填写，且只能大小写字符和数字下划线组成，长度为3~10个字符');
     history.back()
</script>";
        exit;
    }
}

//个人修改资料密码修改的程序
if(!empty($pw)) {
    if ($pw <> $cpm) {
        echo "<script>alert('密码和确认密码必须相同');
     history.back()
</script>";
        exit;
    } else {

        if (!preg_match('/^[a-zA-Z0-9_*]{6,10}$/', $pw)) {
//            preg_match：正则表达式的匹配值是否相同
            echo "<script>alert('密码必须填写，且只能大小写字符和数字下划线和*组成，长度为6~10个字符two');
     history.back()
</script>";
            exit;
        }
    }
}
if(!empty($email)){
    if(!preg_match('/^[a-zA-Z0-9_\-]+@([a-zA-Z0-9]+\.)(com|cn|net|org)$/', $email)){
        echo "<script>alert('邮箱格式不正确');
     history.back()
</script>";
        exit;
    }
}




//判断用户名是否重复
//$sql ="select * from info where username = '$userName'";
include_once  "conn.php";
if($pw){
    $sql = "update info set pw='".md5($pw)."',email='$email',sex='$sex',fav='$fav' where username = '$userName'";
    $url = 'logout.php';
}else{
    $sql = "update info set email='$email',sex='$sex',fav='$fav' where username = '$userName'";
    $url = 'index.php';
}
//判断数据原source是否为管理员
if($source == 'admin'){
//    跳转到管理员后台
    $url = 'admin.php?page='.$page;
}
$result = mysqli_query($conn, $sql);//返回一个记录表

//
if($result){
    echo "<script>alert('更新个人数据成功');
    location.href='$url';
</script>";
}else{
    echo "<script>alert('更新个人数据失败');history.back();
   
</script>";
}
//$num = mysqli_num_rows($result);
//if($num){
//    echo "<script>alert('此用户名已被占用，请重新输入');
//     history.back()
//</script>";
//    exit;
//
//
//
//}

//$sql = "insert into info (username, pw, sex, email, fav, createTime) values ('$userName', '".md5($pw)."', '$sex', '$email', '$fav', '".time()."')";
//执行查询语句//
////$result = mysqli_query($conn, $sql);
////if($result){
////    echo "<script>alert('数据插入成功');location.href='index.php'</script>";
////}else{
////    echo "<script>alert('数据插入失败');</script>";
////}

